package com.yuntian.controller.admin;

import com.yuntian.entity.Categories;
import com.yuntian.result.Result;
import com.yuntian.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类
     */
    @GetMapping("/list")
    public Result<List<Categories>> getAllCategories() {
        List<Categories> categories = categoryService.selectList();
        return Result.success(categories);
    }

    /**
     * 新增分类
     */
    @PostMapping("/add")
    public Result addCategory(@RequestBody Categories categories) {
        categoryService.insertCategory(categories);
        return Result.success();
    }

    /**
     * 修改分类
     */
    @PutMapping("/update")
    public Result updateCategory(@RequestBody Categories categories) {
        categoryService.updateCategory(categories);
        return Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 商品类别数量
     */
    @GetMapping("/count")
    public Result<Integer> count(){
        Integer count = categoryService.count();
        return Result.success(count);
    }
}
